/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb.common;

import entity.AddettoNegozio;
import facade.AddettoNegozioFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Antonio
 */
@Stateless
public class AddettoManager implements AddettoManagerLocal {

    @EJB
    private AddettoNegozioFacade addettoFacade;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext(unitName = "E-Market-ejbPU")
    private EntityManager em;

    @Override
    public void creaAddetto(AddettoNegozio addetto) {
        Query q = em.createNamedQuery("persona.cercaPerCodiceFiscale");
        q.setParameter(1, addetto.getCodiceFiscale());
        if (!q.getResultList().isEmpty()) {

            return;
        }
        addettoFacade.create(addetto);
    }// creaAddetto

    public void persist(Object object) {
        em.persist(object);
    }

    @Override
    public void rimuoviAddetto(AddettoNegozio addetto) {
        Query q = em.createNamedQuery("persona.cercaPerCodiceFiscale");
        q.setParameter(1, addetto.getCodiceFiscale());
        if (!q.getResultList().isEmpty()) {

            return;
        }
        addettoFacade.remove(addetto);
    }// rimuoviAddetto

    @Override
    public AddettoNegozio cercaPerId(Long id) {

        if (id == null) {

            return null;
        }

        return addettoFacade.find(id);
    }// cercaPerId

    @Override
    public List<AddettoNegozio> cercaTutto() {

        return addettoFacade.findAll();
    }// cercaTutto
}
